import EventMgr from "../../../Core/Event/EventMgr";
import Log from "../../../Core/Log/Log";
import SdkTool from "../../../Core/Sdk/SdkTool";
import TimerMgr from "../../../Core/Timer/TimerMgr";
import ProtoBufManager from "../../proto/ProtoBufManager";
import { common, lobby } from "../../proto/protoCmd";
import { ViewManager } from "../base/ViewManager";
import { AbstractModel } from "../socket/AbstractModel";
import { GameModels } from "../socket/GameModels";
import { SocketManager } from "../socket/SocketManager";
import ModuleWindow from "../window/ModuleWindow";
import WindowObject from "../window/WindowObject";
import { ModelPlayerInfo } from "./ModelPlayerInfo";

export class ModelLobby extends AbstractModel {
    public roomPlayerState: number;//0none 1房主 2玩家
    public RoomId: string = ``;
    public GameId: number;
    public GameRoute: string;//房间路由
    public OtherPlayer: ModelPlayerInfo = null;
    /**随机匹配游戏 */
    public send_ReqRandomMateGame(): void {
        const msg = ProtoBufManager.ins.lobby.ReqRandomMateGame.create();
        msg.GameId = 1;
        const data = ProtoBufManager.ins.lobby.ReqRandomMateGame.encode(msg).finish();
        Log.ins.log(1, `发起匹配`);
        this.send(2000, GameModels.info.playerInfo.LobbyRoute + `.ReqRandomMateGame`, data);
    }
    /**创建房间 */
    public send_ReqCreateRoom(): void {
        const msg = ProtoBufManager.ins.lobby.ReqCreateRoom.create();
        msg.GameId = 1;
        const data = ProtoBufManager.ins.lobby.ReqCreateRoom.encode(msg).finish();
        this.send(2001, GameModels.info.playerInfo.LobbyRoute + `.ReqCreateRoom`, data);
    }
    /**加入房间 */
    public send_ReqEnterRoom(RoomId: string): void {
        const msg = ProtoBufManager.ins.lobby.ReqEnterRoom.create();
        msg.GameId = 1;
        msg.RoomId = RoomId;
        msg.InviteUid = GameModels.info.playerInfo.UserId;
        const data = ProtoBufManager.ins.lobby.ReqEnterRoom.encode(msg).finish();
        this.send(2002, GameModels.info.playerInfo.LobbyRoute + `.ReqEnterRoom`, data);
    }
    /**上传名称头像信息 */
    public send_ReqUploadWXInfo(): void {
        const msg = ProtoBufManager.ins.lobby.ReqUploadWXInfo.create();
        msg.UId = GameModels.info.playerInfo.UserId;
        msg.NickName = SdkTool.ins.nickName;
        msg.WXAvatar = SdkTool.ins.avatarUrl;
        const data = ProtoBufManager.ins.lobby.ReqUploadWXInfo.encode(msg).finish();
        this.send(2003, GameModels.info.playerInfo.LobbyRoute + `.ReqUploadWXInfo`, data);
    }
    /**
     * 请求记录数据信息
     */
    public send_ReqRecordData(): void {
        const msg = ProtoBufManager.ins.lobby.ReqRecordData.create();
        const data = ProtoBufManager.ins.lobby.ReqRecordData.encode(msg).finish();
        this.send(2004, GameModels.info.playerInfo.LobbyRoute + `.ReqRecordData`, data);
    }
    public send_ReqGM(str: string, param: string[]): void {
        const msg = ProtoBufManager.ins.lobby.ReqGM.create();
        msg.GMCode = str;
        msg.Params = param;
        const data = ProtoBufManager.ins.lobby.ReqGM.encode(msg).finish();
        this.send(2005, GameModels.info.playerInfo.LobbyRoute + `.ReqGM`, data);
    }
    public resetGame(): void {
        GameModels.lobby.OtherPlayer = null;
        GameModels.lobby.RoomId = ``;
        GameModels.game.Round = 0;
    }
    protected registerInterestedProtocols(): void {
        this.reg(2000, this.onRespRandomMateGame, `lobby`, `RespRandomMateGame`);
        this.reg(2001, this.onRespCreateRoom, `lobby`, `RespCreateRoom`);
        this.reg(2002, this.onRespEnterRoom, `lobby`, `RespEnterRoom`);
        this.reg(2003, this.onResUploadWXInfo, `lobby`, `RespUploadWXInfo`);
        this.reg(2004, this.onRespRecordData, `lobby`, `RespRecordData`);
        this.reg(2005, this.onRespGM, `lobby`, `RespGM`);
    }
    public regRouteMap(): void {
        // this.addRouteMap(GameModels.info.playerInfo.LobbyRoute + `.SyncUserBaseInfo`, this.onSyncUserBaseInfo);
        this.addRouteMap(GameModels.info.playerInfo.LobbyRoute + `.SyncGameMateSuccess`, this.onSyncGameMateSuccess, `lobby`, `SyncGameMateSuccess`);
    }
    public removeRouteMap(): void {
        SocketManager.ins.unRouteHandler(GameModels.info.playerInfo.LobbyRoute + `.SyncGameMateSuccess`);
    }
    private onRespGM(info: lobby.RespGM): void {
        Log.ins.log(1, info);
        ViewManager.ins.showtips(`GM使用成功`);
    }
    private onRespRecordData(info: lobby.RespRecordData): void {
        GameModels.info.playerInfo.recordData = info.Data;
        EventMgr.emit(`event_RespRecordData`);
    }
    private onResUploadWXInfo(info: lobby.RespUploadWXInfo): void {
        GameModels.info.playerInfo.NickName = SdkTool.ins.nickName;
        GameModels.info.playerInfo.Avatar = SdkTool.ins.avatarUrl;
    }
    private onRespEnterRoom(info: lobby.RespEnterRoom): void {
        this.GameId = info.GameId;
        this.RoomId = info.RoomId;
        this.GameRoute = info.GameRoute;
        if (!this.OtherPlayer) {
            this.OtherPlayer = new ModelPlayerInfo();
        }
        this.OtherPlayer.NickName = info.OtherPlayer.NickName;
        this.OtherPlayer.Avatar = info.OtherPlayer.Avatar;
        this.OtherPlayer.UserId = info.OtherPlayer.Uid;
        this.OtherPlayer.recordData = info.OtherPlayer.Records;

        this.roomPlayerState = 2;
        Log.ins.log(1, `加入房间成功,房间路由：${this.GameRoute}`);
        GameModels.game.regRouteMap();
        EventMgr.emit("event_RespEnterRoom");
        ViewManager.ins.showtips("进入房间成功");
        ModuleWindow.ins.openView(WindowObject.ins.PlayView, true);

    }
    private onRespCreateRoom(info: lobby.RespCreateRoom): void {
        this.RoomId = info.RoomId;
        this.GameId = info.GameId;
        this.GameRoute = info.GameRoute;
        this.roomPlayerState = 1;
        this.OtherPlayer = null;
        GameModels.game.regRouteMap();
        Log.ins.log(1, `创建房间成功,房间路由:${this.GameRoute},房间号:${this.RoomId}`);
        EventMgr.emit("event_RespCreateRoom");
    }
    /**游戏匹配成功 */
    private onSyncGameMateSuccess(info: lobby.SyncGameMateSuccess): void {
        this.RoomId = info.RoomId;
        this.GameId = info.GameId;
        this.GameRoute = info.GameRoute;
        if (!this.OtherPlayer) {
            this.OtherPlayer = new ModelPlayerInfo();
        }
        for (let i of info.Players) {
            if (i.Uid != GameModels.info.playerInfo.UserId) {
                this.OtherPlayer.NickName = i.NickName;
                this.OtherPlayer.Avatar = i.Avatar;
                this.OtherPlayer.UserId = i.Uid;
                this.OtherPlayer.recordData = i.Records;
                break;
            }
        }
        GameModels.game.regRouteMap();
        Log.ins.log(1, `游戏匹配成功,房间路由:${this.GameRoute},房间号:${this.RoomId}`);
        EventMgr.emit("event_SyncGameMateSuccess");
    }
    private onRespRandomMateGame(info: lobby.RespRandomMateGame): void {

    }
}